package com.whut.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.whut.common.BaseContext;
import com.whut.common.Message;
import com.whut.entity.Courier;
import com.whut.service.CourierService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName:CourierController
 * @Description:快递员控制类
 * @Author:liumengying
 * @Date: 2022/8/2 21:48
 * Version v1.0
 */
@RestController
@Slf4j
@RequestMapping("/courier")
public class CourierController {

    @Autowired
    private CourierService courierService;


    /**
     * 快递员信息列表
     * @return
     */
//    @PostMapping("/list.do")
//    public List<Courier> list(){
//        LambdaQueryWrapper<Courier> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.orderByDesc(Courier::getLoginTime);
//        List<Courier> list = courierService.list(queryWrapper);
//        return list;
//    }
    @GetMapping("/list.do")
    @ResponseBody
    public List<Courier> page(int offset, int pageNumber){
        log.info("express分页：pageNum={},pageSize={}",offset,pageNumber);
        final LambdaQueryWrapper<Courier> queryWrapper=new LambdaQueryWrapper<>();
        //构造分页条件
       // select * from express limit offset,pageNumber;
        queryWrapper.last("limit "+offset+","+pageNumber);
        queryWrapper.orderByDesc(Courier::getLoginTime);
        //获取分页信息
        List<Courier> expresses=courierService.list(queryWrapper);

        log.info("pagelist={}",expresses.toString());
        return expresses;
    }

    /**
     * 快递员增加
     * @param request
     * @return
     */
    @PostMapping("/insert.do")
    public String addCourier(HttpServletRequest request){

        Courier courier=new Courier();
        courier.setName(request.getParameter("realName"));
        courier.setSysPhone(request.getParameter("tel"));
        courier.setIdCardNumber(request.getParameter("identityId"));
        courier.setPassword(request.getParameter("password"));
        courier.setLoginTime(LocalDateTime.now());
        log.info("进入添加Couriew函数：{}",courier);
        boolean flag=courierService.save(courier);
        return flag ?"success":"false";
    }

    /**
     * 根据电话查找快递员
     * @param request
     * @return
     */
    @PostMapping("/hasUser.do")
    public Message<Courier> hasUser(HttpServletRequest request){

        String phone=request.getParameter("tel");
        log.info("courier进入查询函数，phone={}",phone);
        LambdaQueryWrapper<Courier> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(phone!=null,Courier::getSysPhone,phone);
        Courier courier=courierService.getOne(queryWrapper);
        if(BaseContext.getCurrentId()==null)
        {
            return Message.error("用户未登录");
        }
        return Message.success(courier);
    }


    /**
     * 快递员更新
     * @param courier
     * @return
     */
   @PostMapping("/update.do")
    public String update(@RequestBody Courier courier){
        courier.setLoginTime(LocalDateTime.now());
        log.info("进入更新courier函数，接收参数：{}",courier);
        boolean flag=courierService.updateById(courier);
        return flag ?"success":"false";
   }

    /**
     * 快递员删除
     * @param request
     * @return
     */
   @PostMapping("/delete.do")
    public String delete(HttpServletRequest request){
        String phone=request.getParameter("tel");
        log.info("进入Courier的删除函数，phone={}",phone);
        LambdaQueryWrapper<Courier> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(phone!=null,Courier::getSysPhone,phone);
        boolean flag= courierService.remove(queryWrapper);
        return flag ?"success":"false";
   }

}


